package com.hfjyz.registration.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;

import com.hfjyz.registration.command.SignUpPersonSearchCommand;
import com.hfjyz.registration.contants.ExamContants;
import com.hfjyz.registration.model.Apply;
import com.hfjyz.registration.model.Depart;
import com.hfjyz.registration.model.Exam;
import com.hfjyz.registration.model.Office;
import com.hfjyz.registration.service.RegistService;

public class SignUpPeopleInfoController extends SimpleFormController {

	private RegistService registService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#
	 * referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object,
	 * org.springframework.validation.Errors, int)
	 */
	@SuppressWarnings("rawtypes")
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		SignUpPersonSearchCommand cmd = (SignUpPersonSearchCommand) command;
		Exam exam = (Exam) request.getSession().getAttribute(ExamContants.SESSION_KEY_EXAM_INFO);
		cmd.setExam(exam);
		cmd.setVerifyUserId(null);
		Map model = super.referenceData(request, command, errors);
		if (model == null) {
			model = new HashMap();
		}
		initModel(model, cmd);

		return model;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject
	 * (javax.servlet.http.HttpServletRequest)
	 */
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		HttpSession session = request.getSession();
		Object cmd = session.getAttribute(getFormSessionAttributeName(request));
		if (cmd != null) {
			return cmd;
		}
		return super.formBackingObject(request);
	}

	@SuppressWarnings("rawtypes")
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {
		SignUpPersonSearchCommand cmd = (SignUpPersonSearchCommand) command;
		Exam exam = (Exam) request.getSession().getAttribute(ExamContants.SESSION_KEY_EXAM_INFO);
		cmd.setExam(exam);
		request.getSession().setAttribute(getFormSessionAttributeName(), cmd);
		if (cmd.getVerifyUserId() == null || cmd.getVerifyUserId().isEmpty()) {

			Map model = errors.getModel();
			initModel(model, cmd);
			return new ModelAndView(getFormView(), model);
		} else {
			String url = "verify.action?userId=" + cmd.getVerifyUserId();
			return new ModelAndView(new RedirectView(url));
		}
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void initModel(Map model,
			SignUpPersonSearchCommand cmd) throws Exception {

		List<Depart> deptList = registService.listDepart(cmd.getExam());

		// Depart dept = new Depart();
		// dept.setId(-1);
		// dept.setName("");
		// deptList.add(0, dept);
		model.put("departs", deptList);

		List<Office> officeList = registService.listOffice(cmd.getExam());
		Office office = new Office();
		office.setId(-1);
		office.setName("");
		office.setCode("");
		officeList.add(0, office);
		model.put("offices", officeList);

		if ((cmd.getDeptId() == null || cmd.getDeptId() == -1)
				&& deptList != null && deptList.size() > 0) {
			cmd.setDeptId(deptList.get(0).getId());
		}
		// if ((cmd.getPostId() == null || cmd.getPostId() == -1)
		// && officeList != null && officeList.size() > 0) {
		// cmd.setPostId(officeList.get(0).getId());
		// }
		List<Apply> applyList = registService.listApplyUser(cmd);
		model.put("applyUsers", applyList);
	}

	/**
	 * @return the registService
	 */
	public RegistService getRegistService() {
		return registService;
	}

	/**
	 * @param registService
	 *            the registService to set
	 */
	public void setRegistService(RegistService registService) {
		this.registService = registService;
	}

}